package com.waverider.promotion.web.bg.screen;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;

import com.iacrqq.util.StringUtil;
import com.taobao.api.domain.Item;
import com.waverider.promotion.biz.manager.ItemManager;
import com.waverider.promotion.model.BaseQueryModel;
import com.waverider.promotion.model.ItemQueryModel;
import com.waverider.promotion.model.ResultModel;
import com.waverider.promotion.web.AbstractLoginedController;

public class ItemDisplayList extends AbstractLoginedController {
	private Log logger = LogFactory.getLog(ItemDisplayList.class);
	ItemManager itemManager;

	public final void setItemManager(ItemManager itemManager) {
		this.itemManager = itemManager;
	}

	@Override
	protected ModelAndView handleLogined(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = null;
		Map<String, Object> context = null;

		Long currentPage = null;
		String strCurrentPage = request.getParameter("current_page");
		String promotionId = request.getParameter("promotionId");
		if (StringUtil.isBlank(strCurrentPage)) {
			currentPage = 1L;
		} else {
			try {
				currentPage = Long.valueOf(strCurrentPage);
			} catch (NumberFormatException e) {
				logger.error(e);
				currentPage = 1L;
			}
		}
		ItemQueryModel queryModel = ItemQueryModel.buildQueryModel(currentPage,
				BaseQueryModel.DEFAULT_PAGE_SIZE);
		ResultModel<Item> resultModel = itemManager.getItemList(
				getSessionKey(request.getSession()), queryModel);

		modelAndView = new ModelAndView("/bg/itemDisplayList");
		context = modelAndView.getModel();
		context.put("resultModel", resultModel);
		context.put("promotionList", itemManager
				.getExpirePromitonByShopId(getShopId(request.getSession())));
		context.put("pageNo", 4);
		if (StringUtils.isNotBlank(promotionId))
			context.put("promotionId", Long.valueOf(promotionId));
		context.put("isUnfree", getUnfree(request.getSession()));

		return modelAndView;
	}

}
